<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        canvas{
            border: 1px solid red;
        }
    </style>
</head>
<body>
<canvas id="box" width="800" height="800"></canvas>
<script type="text/javascript">
    var box=document.getElementById("box");
    var pen=box.getContext("2d");

    pen.clearRect(0,0,box.width,box.height);//清除画布
    //先创建线性渐变的对象，然后给对象添加渐变的颜色
    var ele=pen.createLinearGradient(20,20,400,400);
    ele.addColorStop(0,"red");
    ele.addColorStop(0.5,"green");
    ele.addColorStop(1,"blue");

    pen.beginPath();
    pen.moveTo(20,20);
    pen.lineTo(400,400);
    //使用的时候，把线性渐变的对象赋值给边框样式或者是填充样式
    pen.strokeStyle=ele;
    pen.closePath();
    pen.stroke();
  //前三个参数开始渐变的圆心的坐标以及半径
    //后三个参数结束渐变的圆心的坐标以及半径
    var ra= pen.createRadialGradient(400,400,10,400,400,400);
    ra.addColorStop(0,"red");
    ra.addColorStop(0.5,"green");
    ra.addColorStop(1,"blue");

    pen.beginPath();
    pen.arc(400,400,300,0,Math.PI*2);
    pen.fillStyle=ra;
    pen.fill();
    pen.closePath();
    pen.stroke();



</script>
</body>
</html>